//引入工具类
const util = require('../../utils/util');
// 加载配置文件
const config = require('../../utils/config');

const app = getApp()

Page({
    data: {
        pageTitle: '',
        id: '',
        meterStatus: '',
        detailInfo: {},
        isCompany: false,
        isRead: false,
        isMeter: false
    },
    getDetailData() {
        wx.showLoading({
            title: '加载中...'
        })
        var apiUrl = 'company/findById';
        if (this.data.pageTitle == "水表查询") {
            apiUrl = 'meter/findById';
        } else if (this.data.pageTitle == "实时数据") {
            apiUrl = 'actual/findById';
        }
        var params = {
            id: this.data.id
        }
        let _this = this;
        app.ajax({
            url: `${config.httpUrl}${apiUrl}`,
            method: 'get',
            data: params,
            callback: function(error, data) {
                wx.hideLoading();
                _this.setData({
                    detailInfo: data.data
                });
            }
        })
    },
    goCompany(e) {
        let id = e.currentTarget.dataset.id
        if (!this.data.isCompany || !id) {
            return;
        }
        let url = `/pages/detail/detail?name=单位查询&id=${id}`
        wx.navigateTo({
            url
        });
    },
    goMeter(e) {
        let id = e.currentTarget.dataset.id
        if (!this.data.isMeter || !id) {
            return;
        }
        let url = `/pages/detail/detail?name=水表查询&id=${id}`
        wx.navigateTo({
            url
        });
    },
    goRead(e) {
        let dataset = e.currentTarget.dataset;
        if (!this.data.isRead || !dataset.address) {
            return;
        }
        let url = `/pages/read-line/read-line?name=${dataset.name}&meterAddress=${dataset.address}&title=${dataset.title}`
        wx.navigateTo({
            url
        });
    },
    goNav() {
        wx.openLocation({
            latitude: this.data.detailInfo.latitude,
            longitude: this.data.detailInfo.longitude,
            scale: 12,
            name: this.data.detailInfo.name,
            address: this.data.detailInfo.address
        })
    },
    onLoad: function(options) {
        this.setData({
            isCompany: app.globalData.roleList.indexOf('单位查询') != -1,
            isRead: app.globalData.roleList.indexOf('读数查询') != -1,
            isMeter: app.globalData.roleList.indexOf('水表查询') != -1
        })
        console.log('roleList', this.data.roleList);
        console.log(options)
        var _this = this;
        if (!!options.state) {
            _this.setData({
                meterStatus: options.state || ''
            })
        }
        _this.setData({
            pageTitle: options.name,
            id: options.id
        })
        wx.setNavigationBarTitle({
            title: _this.data.pageTitle
        })
        this.getDetailData()
    },
    onShow() {
        let pages = getCurrentPages();
        let curPage = pages[pages.length - 1];
        app.checkBind(curPage.route);
    }
})